/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package aicue1;

import aicue1.callbackhandlers.ServerPasswordCallbackHandler;
import aicue1.services.BankingServiceImpl;
import aicue1.services.CustomerManagementServiceRestImpl;
import aicue1.services.CustomerManagementServiceSoapImpl;
import aicue1.services.MailService;
import aicue1.services.OrderManagementService;
import aicue1.services.OrderManagementServiceImpl;
import aicue1.services.SMSService;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import javax.xml.ws.Endpoint;
import org.apache.cxf.binding.BindingFactoryManager;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxrs.JAXRSBindingFactory;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;

/**
 *
 * @author Thomas
 */
public class Publisher
{
    public static void main(String[] args)
    {
        System.out.println("Starting services...");

        // SOAP-PUBLISHING

        // SMS-, Mail-, OrderManagement- and CustomerManagement-Service
        Endpoint.publish("http://localhost:8088/smsservice", new SMSService());
        Endpoint.publish("http://localhost:8089/mailservice", new MailService());
        Endpoint.publish("http://localhost:8083/orderservice", new OrderManagementServiceImpl());
        Endpoint.publish("http://localhost:8086/customersoap", new CustomerManagementServiceSoapImpl());
        
        // Banking-Service
        EndpointImpl jaxWsEndpoint = (EndpointImpl) Endpoint.publish("http://localhost:8084/bankingservice", new BankingServiceImpl());
        if(args.length == 0 || args.length > 0 && args[0].equals("true"))
        {
            System.out.println("der prevedel sitzt neben");
            org.apache.cxf.endpoint.Endpoint cxfEndpoint = jaxWsEndpoint.getServer().getEndpoint();
            Map<String,Object> inProps = new HashMap<String,Object>();
            inProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
            inProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT); // password type: plain text
            // properties.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_DIGEST); // for hashed password
            inProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, ServerPasswordCallbackHandler.class.getName()); // callback used to retrieve password for given user
            WSS4JInInterceptor wssIn = new WSS4JInInterceptor(inProps);
            cxfEndpoint.getInInterceptors().add(wssIn);
        }

        // REST-PUBLISHING

        // CustomerMangement-Service
        JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
        sf.setResourceClasses(CustomerManagementServiceRestImpl.class);
        sf.setResourceProvider(CustomerManagementServiceRestImpl.class, new SingletonResourceProvider(new CustomerManagementServiceRestImpl()));
        sf.setAddress("http://localhost:8085/customers");
        BindingFactoryManager manager = sf.getBus().getExtension(BindingFactoryManager.class);
        JAXRSBindingFactory factory = new JAXRSBindingFactory();
        factory.setBus(sf.getBus());
        manager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID, factory);
        sf.create();

        insertTestData();

        System.out.println("Services started. Hit enter to exit.");
        new Scanner(System.in).nextLine();
        System.exit(0);
    }

    public static void insertTestData()
    {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

        factory.setServiceClass(OrderManagementService.class);
        factory.setAddress("http://localhost:8083/orderservice");

        OrderManagementService orderManagementService = (OrderManagementService) factory.create();
        orderManagementService.insert_test_data();
    }
}